package scuba.DiveBuddy.data;

import android.content.ContentValues;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.google.android.gms.maps.model.LatLng;
import scuba.DiveBuddy.Class.DiverProfile;
import scuba.DiveBuddy.Class.LogEntry;

/**
 * @author Alex Anderson
 * @version 1.0
 * @since {10/04/2014}
 */
public class LogEntryPlanSource {
    private static String TABLE_NAME = "LogPlan";
    private static String COLUMN_ID = "ID";
    private static String COLUMN_DIVER = "DiverID";
    private static String COLUMN_DIVEDATE = "Date";
    private static String COLUMN_TIMEIN = "TimeIn";
    private static String COLUMN_TIMEOUT = "TimeOut";
    private static String COLUMN_MAXDEPTH = "MaxDepth";
    private static String COLUMN_DEPTHUNITS = "DepthUnits";
    private static String COLUMN_DIVETYPE = "DiveType";
    private static String COLUMN_ALTITUDE = "Altitude";
    private static String COLUMN_ALTUNITS = "AltitudeUnits";
    private static String COLUMN_AIRMIX = "AirMix";

    public static void CreateTable(SQLiteDatabase db) {
        String create = "create table " + TABLE_NAME  +" (" +
                COLUMN_ID + " integer primary key autoincrement," +
                COLUMN_DIVER + " integer not null," +
                COLUMN_DIVEDATE + " integer not null," +
                COLUMN_TIMEIN + " integer not null," +
                COLUMN_TIMEOUT + " integer not null," +
                COLUMN_MAXDEPTH + " real not null," +
                COLUMN_DEPTHUNITS + " integer not null," +
                COLUMN_DIVETYPE + " integer not null," +
                COLUMN_ALTITUDE + " integer," +
                COLUMN_ALTUNITS + " integer," +
                COLUMN_AIRMIX + " integer not null);";

        Log.w("DATABASE", "Creating table " + TABLE_NAME + " with STATEMENT = '" + create + "'");

        try {
            Log.w("DATABASE", "Created table " + TABLE_NAME);
            db.execSQL(create);
        }
        catch(SQLException sql) {
            Log.e("DATABASE", "Create Failed on " + TABLE_NAME + " with error = '" + sql + "'");
        }
    }
    public static void DropTable(SQLiteDatabase db) {
        String drop = "drop table if exists " + TABLE_NAME;

        Log.w("DATABASE",
                "Dropping table " + TABLE_NAME + " with STATEMENT = '" + drop + "'");

        try {
            Log.w("DATABASE", "Table " + TABLE_NAME + " Dropped");
            db.execSQL(drop);
        }
        catch(SQLException sql) {
            Log.e("DATABASE", "Failed to drop table " + TABLE_NAME + " with error = '" + sql + "'");
        }
    }
    public static void UpgradeTable(SQLiteDatabase db) {
        DropTable(db);
        CreateTable(db);
    }


    private static ContentValues getContentValues(LogEntry entry) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_DIVEDATE,entry.getDiveDate().toDateTimeAtStartOfDay().getMillis());
        values.put(COLUMN_DIVER, DiverProfile.getActiveProfile().getID());
        values.put(COLUMN_TIMEIN, entry.getTimeIn().toDateTimeToday().getMillis());
        values.put(COLUMN_TIMEOUT, entry.getTimeOut().toDateTimeToday().getMillis());
        values.put(COLUMN_MAXDEPTH, entry.getMaxDepth());
        values.put(COLUMN_DEPTHUNITS, entry.getDepthUnits().Value);
        values.put(COLUMN_DIVETYPE, entry.getDiveType().Value);
        values.put(COLUMN_AIRMIX, entry.getAirMix().getID());
        values.put(COLUMN_ALTITUDE, entry.getAltitude());
        values.put(COLUMN_ALTUNITS, entry.getAltitudeUnits().Value);
        return values;
    }
}
